<%-- 
    Document   : manage
    Created on : Sep 21, 2011, 8:20:36 PM
    Author     : SAMMY
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<jsp:include page="/header.jsp"></jsp:include>
<script type="text/javascript">
    $(document).ready(function() {
        $('a.delete').click(function(e) {
            e.preventDefault();
            
            var answer = confirm("Do you want to delete this user?");
            
            if (answer){
                var parent = $(this).parent().parent();
            
                $.ajax({
                    type: 'get',
                    url: 'users',
                    data: 'action=delete&id=' + parent.attr('id').replace('record-', ''),
                    beforeSend: function() {
                        parent.fadeTo('slow', 0.25);
                    },
                    success: function() {
                        parent.find('td').wrapInner('<div style="display: block;" />').parent().find('td > div').slideUp(300, function() {
                            parent.remove();
                        });
                    },
                    error: function() {
                        parent.stop().fadeTo('slow', 1);
                    }
                });
            }
        });
    });
</script>
<h1>Manage Users</h1>
<table>
    <tr>
        <th>ID</th>
        <th>Username</th>
        <th>Full Name</th>
        <th>Role</th>
        <th><a href="./users?action=add">Add Users</a></th>
    </tr>
    <c:forEach items="${users}" var="user">
        <tr id="record-${user.id}">
            <td>${user.id}</td>
            <td>${user.getUsername()}</td>
            <td>${user.getName()}</td>
            <td>${user.getRole()}</td>
            <td><a href="./users?action=edit&id=${user.id}">Edit</a> <a class="delete" href="./users?action=delete&id=${user.id}">Delete</a></td>
        </tr>
    </c:forEach>
</table>
<jsp:include page="/footer.jsp"></jsp:include>